﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.StaticUnitySceneManager
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

#nullable disable
namespace Netick.Unity;

internal static class StaticUnitySceneManager
{
  private static int NumOfPeers;
  private static List<Scene> FreeScenes = new List<Scene>(32 /*0x20*/);
  internal static List<int> InitialScenes = new List<int>(32 /*0x20*/);
  internal static bool IsInitialized;
  private static bool HasInitialList;
  private static Queue<SceneOperation> SceneOperations = new Queue<SceneOperation>(32 /*0x20*/);
  internal static SceneOperation ActiveOp;
  internal static SceneOperation PendingFinishedOp;

  public static void InitPhase1(int numOfPeers)
  {
    StaticUnitySceneManager.SceneOperations.Clear();
    StaticUnitySceneManager.ActiveOp = (SceneOperation) null;
    StaticUnitySceneManager.PendingFinishedOp = (SceneOperation) null;
    StaticUnitySceneManager.InitialScenes = new List<int>(32 /*0x20*/);
    StaticUnitySceneManager.FreeScenes = new List<Scene>(32 /*0x20*/);
    StaticUnitySceneManager.IsInitialized = false;
    StaticUnitySceneManager.HasInitialList = false;
    StaticUnitySceneManager.NumOfPeers = numOfPeers;
  }

  public static void InitPhase2()
  {
    for (int index = 0; index < SceneManager.sceneCount; ++index)
      StaticUnitySceneManager.InitialScenes.Add(SceneManager.GetSceneAt(index).buildIndex);
    for (int index = 0; index < SceneManager.sceneCount; ++index)
      StaticUnitySceneManager.FreeScenes.Add(SceneManager.GetSceneAt(index));
    for (int index1 = 0; index1 < StaticUnitySceneManager.NumOfPeers - 1; ++index1)
    {
      for (int index2 = 0; index2 < StaticUnitySceneManager.InitialScenes.Count; ++index2)
      {
        LocalPhysicsMode localPhysicsMode = new LoadSceneParameters(LoadSceneMode.Additive, Network.Config.PhysicsType != PhysicsType.None ? (Network.Config.PhysicsType == PhysicsType.Physics2D ? LocalPhysicsMode.Physics2D : LocalPhysicsMode.Physics3D) : LocalPhysicsMode.None).localPhysicsMode;
        Scene scene = SceneManager.LoadScene(StaticUnitySceneManager.InitialScenes[index2], new LoadSceneParameters(LoadSceneMode.Additive, localPhysicsMode));
        StaticUnitySceneManager.FreeScenes.Add(scene);
      }
    }
  }

  public static void Update()
  {
    if (!StaticUnitySceneManager.IsInitialized)
    {
      for (int index = 0; index < SceneManager.sceneCount; ++index)
      {
        Scene sceneAt = SceneManager.GetSceneAt(index);
        if (!sceneAt.IsValid())
          return;
        sceneAt = SceneManager.GetSceneAt(index);
        if (!sceneAt.isLoaded)
          return;
      }
    }
    if (!StaticUnitySceneManager.HasInitialList)
    {
      StaticUnitySceneManager.InitPhase2();
      StaticUnitySceneManager.HasInitialList = true;
    }
    StaticUnitySceneManager.IsInitialized = true;
    if (StaticUnitySceneManager.PendingFinishedOp != null)
    {
      NetworkSandbox sandbox = StaticUnitySceneManager.PendingFinishedOp.Sandbox;
      if (sandbox.IsRunning && (!sandbox.IsClient || sandbox.IsConnected))
        return;
      StaticUnitySceneManager.PendingFinishedOp = (SceneOperation) null;
    }
    if (StaticUnitySceneManager.ActiveOp == null)
    {
      if (StaticUnitySceneManager.SceneOperations.Count <= 0)
        return;
      StaticUnitySceneManager.ActiveOp = StaticUnitySceneManager.SceneOperations.Dequeue();
      StaticUnitySceneManager.ActiveOp.Start();
    }
    else
      StaticUnitySceneManager.ActiveOp.Update();
  }

  public static bool TryConsumeFreeScene(int sceneBuildIndex, out Scene freeScene)
  {
    freeScene = StaticUnitySceneManager.FreeScenes.Find((Predicate<Scene>) (x => x.buildIndex == sceneBuildIndex));
    StaticUnitySceneManager.FreeScenes.Remove(freeScene);
    return freeScene.buildIndex == sceneBuildIndex;
  }

  internal static void ValidateSceneId(NetworkSandbox sandbox, int netickSceneId)
  {
    int num = SceneManager.sceneCountInBuildSettings + ((UnityEngine.Object) sandbox.NetworkSceneHandler != (UnityEngine.Object) null ? sandbox.NetworkSceneHandler.CustomScenesCount : 0);
    if (netickSceneId < 0 || netickSceneId > num - 1)
      throw new IndexOutOfRangeException($"Desired scene index {netickSceneId} is out of range.");
  }

  internal static bool IsCustomScene(int netickSceneId)
  {
    return netickSceneId >= SceneManager.sceneCountInBuildSettings;
  }

  public static void SwitchScene(
    NetworkSandbox sandbox,
    int buildIndex,
    int sceneCounter,
    HashSet<Scene> loadedScenes)
  {
    if (!sandbox.HasLoadScene)
      return;
    StaticUnitySceneManager.SceneOperations.Enqueue((SceneOperation) new SceneSwitchOperation()
    {
      SceneIndex = buildIndex,
      Sandbox = sandbox,
      PrevScenes = loadedScenes,
      SceneCounter = sceneCounter
    });
  }

  internal static void UnloadScene(NetworkSandbox sandbox, Scene scene)
  {
    if ((UnityEngine.Object) sandbox.NetworkSceneHandler == (UnityEngine.Object) null)
      return;
    if (scene.buildIndex == -1)
      sandbox.NetworkSceneHandler.InvokeUnloadCustomSceneAsync(scene);
    else
      sandbox.NetworkSceneHandler.InvokeUnloadBuildSceneAsync(scene);
  }

  public static void LoadAdditiveScene(NetworkSandbox sandbox, SceneInfo sceneInfo, int slotIndex)
  {
    StaticUnitySceneManager.SceneOperations.Enqueue((SceneOperation) new AdditiveSceneOperation()
    {
      SlotIndex = slotIndex,
      Sandbox = sandbox,
      Load = true,
      SceneIndex = sceneInfo.SceneIndex,
      LoadCounterId = sceneInfo.LoadId,
      LocalPhysicsMode = sceneInfo.LocalPhysicsMode
    });
  }

  public static void UnloadAdditiveScene(NetworkSandbox sandbox, NetworkScene scene, int slotIndex)
  {
    sandbox.Scenes.Remove(scene.Scene);
    StaticUnitySceneManager.SceneOperations.Enqueue((SceneOperation) new AdditiveSceneOperation()
    {
      SlotIndex = slotIndex,
      Sandbox = sandbox,
      Load = false,
      LoadCounterId = scene.LoadCounterId,
      SceneToUnload = scene.Scene
    });
  }

  public static void Reset()
  {
    StaticUnitySceneManager.SceneOperations.Clear();
    StaticUnitySceneManager.ActiveOp = (SceneOperation) null;
  }
}
